package com.hand.qt24953.order.infra.repository.impl;

import com.hand.qt24953.order.api.dto.OrderConditionDTO;
import com.hand.qt24953.order.api.dto.OrderDTO;
import com.hand.qt24953.order.api.dto.OrderLineDTO;
import com.hand.qt24953.order.constant.OrderStatus;
import com.hand.qt24953.order.domain.entity.SoHeader;
import com.hand.qt24953.order.domain.repository.SoHeaderRepository;
import com.hand.qt24953.order.infra.mapper.SoHeaderMapper;
import io.choerodon.core.domain.Page;
import io.choerodon.core.domain.PageInfo;
import io.choerodon.mybatis.pagehelper.PageHelper;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import org.hzero.boot.platform.code.builder.CodeRuleBuilder;
import org.hzero.boot.platform.code.constant.CodeConstants;
import org.hzero.boot.platform.lov.annotation.ProcessLovValue;
import org.hzero.export.vo.ExportParam;
import org.hzero.mybatis.base.impl.BaseRepositoryImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;

/**
 * 销售订单头 资源库实现
 *
 * @author tao.qiu01@hadn-china.com 2019-08-02 11:30:00
 */
@Component
public class SoHeaderRepositoryImpl extends BaseRepositoryImpl<SoHeader> implements SoHeaderRepository {
    private final SoHeaderMapper soHeaderMapper;
    private final CodeRuleBuilder codeRuleBuilder;

    @Autowired(required = false)
    public SoHeaderRepositoryImpl(SoHeaderMapper soHeaderMapper, CodeRuleBuilder codeRuleBuilder) {
        this.soHeaderMapper = soHeaderMapper;
        this.codeRuleBuilder = codeRuleBuilder;
    }


    @Override
    @SuppressWarnings("unchecked")
    public Page<OrderDTO> selectByOrderCondition(PageRequest pageRequest, OrderConditionDTO orderConditionDTO) {
        PageInfo pageInfo = PageHelper.startPage(pageRequest.getPage(), pageRequest.getSize());
        PageHelper.startSort(pageRequest.getSort());
        List ids = soHeaderMapper.selectIdByCondition(orderConditionDTO);
        List<OrderDTO> list;
        if (ids.size() > 0) {
            list = soHeaderMapper.selectByOrderHeaderId(ids);
            //计算总金额
            for (OrderDTO order : list) {
                BigDecimal orderAmount = new BigDecimal("0.0");
                List<OrderLineDTO> lineList = Optional.ofNullable(order.getLines()).orElse(Collections.EMPTY_LIST);
                for (OrderLineDTO line : lineList) {
                    BigDecimal lineAmount = line.getOrderQuantity().multiply(line.getUnitSellingPrice());
                    line.setLineAmount(lineAmount);
                    orderAmount = orderAmount.add(lineAmount);
                }
                order.setOrderAmount(orderAmount);
            }
        } else {
            list = new ArrayList<>(0);
        }
        return new Page<>(list, pageInfo, pageInfo.getTotal());
    }

    @Override
    @ProcessLovValue
    public Page<OrderDTO> export(OrderConditionDTO orderConditionDTO, ExportParam exportParam, PageRequest pageRequest) {
        return selectByOrderCondition(pageRequest, orderConditionDTO);
    }

    @Override
    public int insertSelective(Long tenantId, SoHeader record) {
        String code = codeRuleBuilder.generateCode(tenantId, "HZERO.ORDER.24953.NUMBER",
                CodeConstants.CodeRuleLevelCode.GLOBAL, CodeConstants.CodeRuleLevelCode.GLOBAL, null);
        record.setOrderNumber(code);
        record.setOrderStatus(OrderStatus.NEW.toString());
        return super.insertSelective(record);
    }
}
